﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Common.BLL;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Collections.Generic;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.SoldierArchives.BLL;
using System.Drawing;
using System.IO;
using Yipusoft.Common.Model;

public partial class SoldierArchive_NewEmployee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindNation();
            BindDuty();
            //BindRank();
            BindSpeciality();
            BindProvince();
            BindBackground();
            BindCulture();
            BindReligion();
            BindCities(ddlProvinces.SelectedValue);
            BindSpeciality(ddlSpeciality1.SelectedValue);
            if (String.IsNullOrEmpty(Request["ID"]))
                BindDepartment("203001", "203002", "203003");
            else
                BindDepartment("203001", "203004", "203005");

            if (ddlSection.Items.Count == 0)
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            BindSoldiers();
            if (!String.IsNullOrEmpty(Request["ID"]))
            {
                if (!(Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "203001") || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "203004") || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "203005")))
                {
                    Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                    return;
                }
                btnSave.Visible = false;
                btnUpdate.Visible = true;
                ActionTitle.Text = "更新合同制队员档案";
                BindSoldier(Request["ID"]);
            }


        }
    }

    private void BindSoldier(string soldierID)
    {
        Soldier soldier = new Soldier();
        SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(soldierID);
        SoldierName.Text = soldierInfo.SoldierName;
        ddlSex.SelectedValue = soldierInfo.Sex;

        ddlBackground.SelectedValue = soldierInfo.Background;
        Birthday.Text = soldierInfo.Birthday == DateTime.MinValue ? "" : soldierInfo.Birthday.ToString("yyyy-MM-dd");
        CardID.Text = soldierInfo.CardID;
        //ChooseID.Text = soldierInfo.ChooseID;
        ConfirmID.Text = soldierInfo.ConfirmID;
        ddlCulture.SelectedValue = soldierInfo.Culture;
        bool isexist = false;

        //---------单位-------
        ddlDepartment.SelectedValue = soldierInfo.DepartmentID.Substring(0, 4) + "000";
        bindSelection();
        for (int i = 0; i < ddlSection.Items.Count; i++)
        {
            if (ddlSection.Items[i].Value.Trim() == soldierInfo.DepartmentID.Trim())
            {
                isexist = true;
            }
        }

        if (!isexist)
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }

        ddlSection.SelectedValue = soldierInfo.DepartmentID.Trim();
        //---------单位-------

        ddlDuty.SelectedValue = soldierInfo.Duty;
        Height.Text = soldierInfo.Height == 0 ? "" : soldierInfo.Height.ToString();
        HomePlace.Text = soldierInfo.HomePlace;
        //soldierInfo.IsContract = false;
        JoinPlace.Text = soldierInfo.JoinPlace;
        JoinDate.Text = soldierInfo.JoinTime == DateTime.MinValue ? "" : soldierInfo.JoinTime.ToString("yyyy-MM-dd");
        ddlMerriage.SelectedValue = soldierInfo.Marriage;
        ddlNation.SelectedValue = soldierInfo.Nation;
        ddlProvinces.SelectedValue = CodeItem.GetCodeItem(soldierInfo.Nation).ItemLevel == 2 ? CodeItem.GetCodeItem(soldierInfo.Nation).ParentID : CodeItem.GetCodeItem(CodeItem.GetCodeItem(soldierInfo.Nation).ParentID).ParentID;
        BindCities(ddlProvinces.SelectedValue);
        if (soldierInfo.Native != null && soldierInfo.Native != "")
        {
            CodeItemInfo Nation = CodeItem.GetCodeItem(soldierInfo.Native);
            if (Nation.ItemLevel == 2)
            {
                ddlProvinces.SelectedValue = Nation.ParentID;

            }
            else if (Nation.ItemLevel == 3)
            {
                ddlProvinces.SelectedValue = CodeItem.GetCodeItem(Nation.ParentID).ParentID;
            }
            BindCities(ddlProvinces.SelectedValue);
            ddlCities.SelectedValue = soldierInfo.Native;
        }
        ////soldierInfo.Native = ddlCities.SelectedValue;
        PartyDate.Text = soldierInfo.PartyTime == DateTime.MinValue ? "" : soldierInfo.PartyTime.ToString("yyyy-MM-dd");
        //ddlRank.SelectedValue = soldierInfo.Rank;
        //RegisterID.Text = soldierInfo.RegisterID;
        ddlReligion.SelectedValue = soldierInfo.Religion;


        ddlSpeciality1.SelectedValue = soldierInfo.Speciality1;
        ////soldierInfo.Speciality2 = ddlSpeciality2.SelectedValue;
        BindSpeciality(ddlSpeciality1.SelectedValue);
        ddlSpeciality2.SelectedValue = soldierInfo.Nation;
        ddlDuty.SelectedValue = soldierInfo.Status;
        Weight.Text = soldierInfo.Weight == 0.0m ? "" : soldierInfo.Weight.ToString();
        YouthDate.Text = soldierInfo.YouthTime == DateTime.MinValue ? "" : soldierInfo.YouthTime.ToString("yyyy-MM-dd");
        IsContract.Checked = soldierInfo.IsContract;
    }

    private void BindSoldiers()
    {
        Soldier soldier = new Soldier();
        gvSoldiers.DataSource = soldier.GetSoldiersByWhere("IsDelete=0 and IsContract=1 and setTime>='" + DateTime.Today + "' and SetAID='" + User.Identity.Name + "'", "SetTime", "Desc", 10, 0);
        gvSoldiers.DataBind();
    }

    private void InitDate()
    {
        SoldierName.Text = "";
        ddlSex.SelectedIndex = 0;
        ddlBackground.SelectedIndex = 0;
        Birthday.Text = "";
        CardID.Text = "";
        //ChooseID.Text = "";
        ConfirmID.Text = "";
        ddlCulture.SelectedIndex = 0;
        ddlDepartment.SelectedIndex = 0;
        ddlDuty.SelectedIndex = 0;
        Height.Text = "";
        HomePlace.Text = "";
        //soldierInfo.IsContract = true;
        JoinPlace.Text = "";
        JoinDate.Text = "";
        ddlMerriage.SelectedIndex = 0;
        ddlNation.SelectedIndex = 0;
        ddlProvinces.SelectedIndex = 0;
        BindCities(ddlProvinces.SelectedValue);
        //ddlCities.SelectedValue = soldierInfo.Nation;
        ////soldierInfo.Native = ddlCities.SelectedValue;
        PartyDate.Text = "";
        //ddlRank.SelectedIndex = 0;
        //RegisterID.Text = "";
        ddlReligion.SelectedIndex = 0;


        ddlSpeciality1.SelectedIndex = 0;
        ////soldierInfo.Speciality2 = ddlSpeciality2.SelectedValue;
        BindSpeciality(ddlSpeciality1.SelectedValue);
        ddlDuty.SelectedIndex = 0;
        Height.Text = "";
        Weight.Text = "";
        YouthDate.Text = "";
    }

    private void BindNation()
    {
        ddlNation.DataSource = CodeItem.GetList("ParentID='MZ00' ORDER BY SeqID");
        ddlNation.DataTextField = "ItemName";
        ddlNation.DataValueField = "ItemID";
        ddlNation.DataBind();
        BindStatus();
    }

    private void BindDuty()
    {
        ddlDuty.DataSource = CodeItem.GetList("ParentID='SBZW00' ORDER BY SeqID");
        ddlDuty.DataTextField = "ItemName";
        ddlDuty.DataValueField = "ItemID";
        ddlDuty.DataBind();
    }

    //private void BindRank()
    //{
    //    ddlRank.DataSource = CodeItem.GetList("ParentID='SBJX00' ORDER BY SeqID");
    //    ddlRank.DataTextField = "ItemName";
    //    ddlRank.DataValueField = "ItemID";
    //    ddlRank.DataBind();
    //}

    private void BindSpeciality()
    {
        ddlSpeciality1.DataSource = CodeItem.GetList("ParentID='ZY0000' ORDER BY SeqID");
        ddlSpeciality1.DataTextField = "ItemName";
        ddlSpeciality1.DataValueField = "ItemID";
        ddlSpeciality1.DataBind();
    }

    private void BindProvince()
    {
        ddlProvinces.DataSource = CodeItem.GetList("ParentID='SS000000' ORDER BY SeqID");
        ddlProvinces.DataTextField = "ItemName";
        ddlProvinces.DataValueField = "ItemID";
        ddlProvinces.DataBind();
    }

    private void BindBackground()
    {
        ddlBackground.DataSource = CodeItem.GetList("ParentID='JTCS00' ORDER BY SeqID");
        ddlBackground.DataTextField = "ItemName";
        ddlBackground.DataValueField = "ItemID";
        ddlBackground.DataBind();
    }

    private void BindStatus()
    {
        ddlDuty.DataSource = CodeItem.GetList("ParentID='SBBRCF00' ORDER BY SeqID");
        ddlDuty.DataTextField = "ItemName";
        ddlDuty.DataValueField = "ItemID";
        ddlDuty.DataBind();
    }

    private void BindCulture()
    {
        ddlCulture.DataSource = CodeItem.GetList("ParentID='WHCD00' ORDER BY SeqID");
        ddlCulture.DataTextField = "ItemName";
        ddlCulture.DataValueField = "ItemID";
        ddlCulture.DataBind();
    }

    private void BindReligion()
    {
        ddlReligion.DataSource = CodeItem.GetList("ParentID='ZJXY00' ORDER BY SeqID");
        ddlReligion.DataTextField = "ItemName";
        ddlReligion.DataValueField = "ItemID";
        ddlReligion.DataBind();
    }
    protected void ddlProvinces_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCities(ddlProvinces.SelectedValue);
    }

    private void BindCities(string province)
    {
        ddlCities.DataSource = CodeItem.GetList("ParentID='" + province + "' Order by SeqID");
        ddlCities.DataTextField = "ItemName";
        ddlCities.DataValueField = "ItemID";
        ddlCities.DataBind();
    }
    protected void ddlSpeciality1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSpeciality(ddlSpeciality1.SelectedValue);
    }

    private void BindSpeciality(string speciality)
    {
        ddlSpeciality2.DataSource = CodeItem.GetList("ParentID='" + speciality + "' Order by SeqID");
        ddlSpeciality2.DataTextField = "ItemName";
        ddlSpeciality2.DataValueField = "ItemID";
        ddlSpeciality2.DataBind();
    }

    private void BindDepartment(string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        //ddlDepartment.DataSource = GetDepartmentList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID, permissionAdmin, permissionLevel, permissionUnit);
        //ddlDepartment.DataTextField = "FullName";
        //ddlDepartment.DataValueField = "DepartmentID";
        //ddlDepartment.DataBind();

        string departmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        ddlDepartment.DataSource = Department.GetList(" DepartmentId like '%000' or departmentId ='1312100' or departmentId='1313100'");
        ddlDepartment.DataBind();

        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
        {
            bindSelection();
        }
        else if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel))
        {
            ddlDepartment.SelectedValue = departmentID.Substring(0, 4) + "000";
            ddlDepartment.Enabled = false;
            bindSelection();
        }
        else
            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
            {
                ddlDepartment.SelectedValue = departmentID.Substring(0, 4) + "000";
                ddlDepartment.Enabled = false;
                bindSelection();
                ddlSection.SelectedValue = departmentID;
                ddlSection.Enabled = false;
            }

    }

    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
            departmentList = Department.GetList("1=1");
        else

            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel))
                departmentList = Department.GetList("DepartmentID like '" + departmentID.Substring(0, 4) + "%'");
            else
                if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
                    departmentList = Department.GetList("DepartmentID='" + departmentID + "'");

        return departmentList;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string photo = UploadPhoto(200, 250);
        if (photo == "%")
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "上传照片不支持此文件类型!";
            return;
        }
        if (photo == "-")
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "超过允许的图片尺寸(200×250)范围!";
            return;
        }
        if (photo == "#")
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "上传文件出错!";
            return;
        }
        if (ddlDepartment.SelectedValue == "")
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "对不起,部门不能为空!";
            return;
        }
        SoldierArchiveInfo soldierInfo = new SoldierArchiveInfo();
        soldierInfo.Picture = photo;
        AssignSoldierInfo(soldierInfo);
        Soldier soldier = new Soldier();
        if (soldier.Insert(soldierInfo, User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName))
        {
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "添加合同制士兵";
            log.ItemID = soldierInfo.SoldierName;
            log.ItemName = "BasicBase";
            log.ItemFullUrl = "SoldierArchive/NewEmployee.aspx";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);

            lblInfo.ForeColor = Color.Black;
            lblInfo.Text = "保存成功";
            BindSoldiers();
            InitDate();
        }
        else
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "保存失败！";
        }
    }

    private void AssignSoldierInfo(SoldierArchiveInfo soldierInfo)
    {
        soldierInfo.Background = ddlBackground.SelectedValue;
        soldierInfo.Birthday = Birthday.Text == "" ? DateTime.MinValue : Convert.ToDateTime(Birthday.Text);
        soldierInfo.CardID = CardID.Text;
        soldierInfo.ChooseID = "";
        soldierInfo.ConfirmID = ConfirmID.Text;
        soldierInfo.Culture = ddlCulture.SelectedValue;

        soldierInfo.DepartmentID = ddlSection.SelectedValue;
        soldierInfo.Duty = ddlDuty.SelectedValue;
        soldierInfo.Height = Height.Text == "" ? 0 : int.Parse(Height.Text);
        soldierInfo.HomePlace = HomePlace.Text;
        soldierInfo.IsContract = IsContract.Checked;
        soldierInfo.JoinPlace = JoinPlace.Text;
        soldierInfo.JoinTime = JoinDate.Text == "" ? DateTime.MinValue : Convert.ToDateTime(JoinDate.Text);
        soldierInfo.Marriage = ddlMerriage.SelectedValue;
        soldierInfo.Nation = ddlNation.SelectedValue;
        soldierInfo.Native = ddlCities.SelectedValue;
        soldierInfo.PartyTime = PartyDate.Text == "" ? DateTime.MinValue : Convert.ToDateTime(PartyDate.Text);
        soldierInfo.Rank = "";
        soldierInfo.RegisterID = "";
        soldierInfo.Religion = ddlReligion.SelectedValue;
        soldierInfo.Sex = ddlSex.SelectedValue;
        string Name = SoldierName.Text;
        Name = Name.Replace(" ", "");
        soldierInfo.SoldierName = Name;
        soldierInfo.Speciality1 = ddlSpeciality1.SelectedValue;
        soldierInfo.Speciality2 = ddlSpeciality2.SelectedValue;
        soldierInfo.Status = ddlDuty.SelectedValue;
        soldierInfo.Weight = Weight.Text == "" ? 0 : Convert.ToDecimal(Weight.Text);
        soldierInfo.YouthTime = YouthDate.Text == "" ? DateTime.MinValue : Convert.ToDateTime(YouthDate.Text);
    }

    /// <summary>
    /// 上传头像
    /// </summary>
    /// <param name="limitWidth"></param>
    /// <param name="limitHeight"></param>
    /// <returns>
    /// %：不支持的上传文件类型
    /// -：超过规定的尺寸
    /// #：上传文件出错
    /// </returns>
    private string UploadPhoto(int limitWidth, int limitHeight)
    {
        string ret = "";
        if (fileUpload.PostedFile != null && fileUpload.PostedFile.ContentLength > 0)
        {
            try
            {
                // if not already present, create a directory named /Uploads/<CurrentUserName>
                string filename = fileUpload.FileName.ToLower();
                int i = filename.LastIndexOf(".");
                filename = filename.Substring(i, filename.Length - i);

                if (!(filename == ".jpg" || filename == ".jpeg" || filename == ".gif" || filename == ".png" || filename == ".bmp"))
                {
                    ret = "%";

                }//检查上传文件的格式是否有效
                else
                {
                    Stream oStream = fileUpload.PostedFile.InputStream;
                    System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

                    int owidth = oImage.Width; //原图宽度
                    int oheight = oImage.Height; //原图高度

                    if (owidth > limitWidth || oheight > limitHeight)
                    {
                        ret = "-";

                    }//检查是否超出规定尺寸
                    else
                    {
                        string dirUrl = this.Request.ApplicationPath +
                           "/Uploads/" + this.Page.User.Identity.Name;
                        string dirPath = Server.MapPath(dirUrl);
                        if (!Directory.Exists(dirPath))
                            Directory.CreateDirectory(dirPath);
                        // save the file under the user's personal folder
                        string fileUrl = dirUrl + "/" + DateTime.Now.ToString("yyMMddHHmmss") + filename;
                        fileUpload.PostedFile.SaveAs(Server.MapPath(fileUrl));
                        //Profile.Initialize(this.Page.User.Identity.Name, true);
                        ret = fileUrl;
                    }
                }
            }
            catch
            {
                ret = "#";
            }
        }
        return ret;
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string photo = UploadPhoto(200, 250);
        if (photo == "%")
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "上传照片不支持此文件类型!";
            return;
        }
        if (photo == "-")
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "超过允许的图片尺寸(200×250)范围!";
            return;
        }
        if (photo == "#")
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "上传文件出错!";
            return;
        }

        if (ddlDepartment.SelectedValue == "")
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "对不起,部门不能为空!";
            return;
        }
        Soldier soldier = new Soldier();
        SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(Request["ID"]);
        if (photo != "")
            soldierInfo.Picture = photo;
        AssignSoldierInfo(soldierInfo);
        if (soldier.Update(soldierInfo, User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName))
        {
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "修改合同制士兵";
            log.ItemID = soldierInfo.SoldierName;
            log.ItemName = "BasicBase";
            log.ItemFullUrl = "SoldierArchive/NewEmployee.aspx";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);

            lblInfo.ForeColor = Color.Black;
            lblInfo.Text = "保存成功。";
            BindSoldiers();
            InitDate();

        }
        else
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "保存失败！";
        }

    }

    protected void gvSoldiers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string accountDepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;

        bool endableDelete = false;
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "203001"))
            endableDelete = true;
        else
            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "203008") && accountDepartmentID.Substring(0, 4) == gvSoldiers.DataKeys[e.RowIndex].Values["DepartmentID"].ToString().Substring(0, 4))
                endableDelete = true;
            else
                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "203009") && accountDepartmentID == gvSoldiers.DataKeys[e.RowIndex].Values["DepartmentID"].ToString())
                    endableDelete = true;
        if (endableDelete)
        {

            Soldier soldier = new Soldier();
            SoldierArchiveInfo info = soldier.GetSoldierInfoByID(gvSoldiers.DataKeys[e.RowIndex].Values["SoldierID"].ToString());
            string name = info.SoldierName;
            if (soldier.Delete(gvSoldiers.DataKeys[e.RowIndex].Values["SoldierID"].ToString(), "人工删除", DateTime.Now, User.Identity.Name, Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName))
            {


                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "删除合同制士兵";
                log.ItemID = name;
                log.ItemName = "BasicBase";
                log.ItemFullUrl = "SoldierArchive/NewEmployee.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);

                lblInfo.ForeColor = Color.Black;
                lblInfo.Text = "删除成功。";
                BindSoldiers();
            }
            else
            {
                lblInfo.ForeColor = Color.Red;
                lblInfo.Text = "删除失败！";
            }
        }
        else
        {
            lblInfo.ForeColor = Color.Red;
            lblInfo.Text = "您无权删除此兵员记录！";
        }
    }
    protected void gvSoldiers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
        e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
        //当鼠标离开的时候 将背景颜色还原的以前的颜色 
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
    }
    protected void btnAutoWrite_Click(object sender, EventArgs e)
    {
        CardCodeInfo cardCodeInfo = CardCode.GetCardCodeInfoByDepartmentID(ddlDepartment.SelectedValue.Substring(0, 4) + "000");
        if (cardCodeInfo != null)
        {
            int len = cardCodeInfo.EmStartNum.Length;
            Soldier soldier = new Soldier();
            for (int i = int.Parse(cardCodeInfo.EmStartNum); i <= int.Parse(cardCodeInfo.EmEndNum); i++)
            {
                string cardID = cardCodeInfo.EmFrontCode + i.ToString().PadLeft(len, '0');
                if (soldier.GetSoldiersCountByWhere("CardID='" + cardID + "' and Left(DepartmentID,4)='" + cardCodeInfo.DepartmentID.Substring(0, 4) + "'") == 0)
                {
                    CardID.Text = cardID;
                    break;
                }

            }
        }
        else
            CardID.Text = "";
    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindSelection();
    }
    void bindSelection()
    {
        if (ddlDepartment.SelectedValue != "")
        {
            string departmentid = ddlDepartment.SelectedValue;
            ddlSection.DataSource = Department.GetList("DepartmentId like '" + departmentid.Substring(0, 4) + "%'");
            ddlSection.DataBind();
        }
        else
        {
            ddlSection.Items.Clear();
        }
    }
}
